using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Tool
{
    public class GlobalVariable
    {
        public string m_name;
        public float m_value;

        public GlobalVariable( string name)
        {
            m_name = name;
            m_value = 0.0f;
        }
    }

    public class GlobalVariableManager
    {
        private System.Windows.Forms.Form m_parent;
        ArrayList m_globalVariables = new ArrayList();

        public GlobalVariableManager(System.Windows.Forms.Form parent)
        {
            m_parent = parent;
        }

        public ArrayList GetGlobalVariables()
        {
            return m_globalVariables;
        }

            // Returns false if there is already a variable of the same name
        public bool AddGlobalVariable(string name)
        {
            for (int i = 0; i < m_globalVariables.Count; ++i)
            {
                if (name == ((GlobalVariable)m_globalVariables[i]).m_name)
                {
                    return false;
                }
            }

            m_globalVariables.Add(new GlobalVariable(name));

            ((Editor)m_parent).GlobalVariableDataGrid.Rows.Add(1);
            ((Editor)m_parent).GlobalVariableDataGrid.Rows[((Editor)m_parent).GlobalVariableDataGrid.Rows.Count - 1].Cells[0].Value = name;
            ((Editor)m_parent).GlobalVariableDataGrid.Rows[((Editor)m_parent).GlobalVariableDataGrid.Rows.Count - 1].Cells[1].Value = 0.0f; // initial value

            return true;
        }

            // Removes a global variable from a sound and from all sounds who were previously using it
        public void RemoveGlobalVariable(string name)
        {
            if (m_globalVariables.Count <= 1 && ((Editor)m_parent).GetEventTree().GetEvents().Count > 0)
            {
                ArrayList evntList = ((Editor)m_parent).GetEventTree().GetEvents();
                for (int i = 0; i < evntList.Count; ++i)
                {
                    if (((Event)evntList[i]).GetSoundList().Count > 0)
                    {
                        ((Editor)m_parent).GetDelVarDlg().ShowDialog();
                        return;
                    }
                }
            }

            for (int i = 0; i < m_globalVariables.Count; ++i)
            {
                if (name == ((GlobalVariable)m_globalVariables[i]).m_name)
                {
                    // we know the name, now we must delete it from all events who are using it
                    TreeNodeCollection nodes = ((Editor)m_parent).EventTree.Nodes;

                    m_globalVariables.RemoveAt(i);
                    ((Editor)m_parent).GlobalVariableDataGrid.Rows.Remove(((Editor)m_parent).GlobalVariableDataGrid.SelectedRows[0]);

                    for (int n = 0; n < nodes.Count; ++n)
                    {
                        for (int child = 0; child < nodes[n].Nodes.Count; ++child)
                        {
                            EventSound currSound = ((Editor)m_parent).GetEventTree().GetEventSound(nodes[n].Text, nodes[n].Nodes[child].Index);

                            if (currSound == null)
                                continue;

                            if (name == currSound.GetVarName(EDIT_TYPE.EDIT_PAN))//m_globalVarName4Pan
                            {
                                //currSound.SetIsUsingVar(EDIT_TYPE.EDIT_PAN, false);// m_isUsingGlobalVar4Pan = false;
                                currSound.SetVarName(EDIT_TYPE.EDIT_PAN, 
                                    ((GlobalVariable)m_globalVariables[0]).m_name);//m_globalVarName4Pan = "";
                            }
                            else if (name == currSound.GetVarName(EDIT_TYPE.EDIT_VOLUME))
                            {
                                //currSound.SetIsUsingVar(EDIT_TYPE.EDIT_VOLUME, false);
                                currSound.SetVarName(EDIT_TYPE.EDIT_VOLUME,
                                    ((GlobalVariable)m_globalVariables[0]).m_name);
                            }
                            else if (name == currSound.GetVarName(EDIT_TYPE.EDIT_DISTORTION))
                            {
                                //currSound.SetIsUsingVar(EDIT_TYPE.EDIT_DISTORTION, false);
                                currSound.SetVarName(EDIT_TYPE.EDIT_DISTORTION,
                                    ((GlobalVariable)m_globalVariables[0]).m_name);
                            }
                            else if (name == currSound.GetVarName(EDIT_TYPE.EDIT_REVERB))
                            {
                                //currSound.SetIsUsingVar(EDIT_TYPE.EDIT_REVERB, false);
                                currSound.SetVarName(EDIT_TYPE.EDIT_REVERB,
                                    ((GlobalVariable)m_globalVariables[0]).m_name);
                            }
                        }
                    }

                    if (name == ((Editor)m_parent).SoundGlobalVarComboBox.Text &&
                        m_globalVariables.Count > 0 )
                    {
                        ((Editor)m_parent).SoundGlobalVarComboBox.SelectedIndex =
                            ((Editor)m_parent).SoundGlobalVarComboBox.FindStringExact( 
                                ((GlobalVariable)m_globalVariables[0]).m_name );
                    }

                    if (((Editor)m_parent).EventTree.SelectedNode == null)
                    {
                        ((Editor)m_parent).SoundEditorPanel.Invalidate();
                        return;
                    }

                        // Now we must update everything
                    if (((Editor)m_parent).EventTree.SelectedNode.Parent == null)
                    {
                        ((Editor)m_parent).SoundEditTypeComboBox.Enabled = false;
                        ((Editor)m_parent).SoundEditorTrackbar.Enabled = false;
                        //((Editor)m_parent).UseGlobalVarCheckbox.Enabled = false;
                        ((Editor)m_parent).SoundGlobalVarComboBox.Enabled = false;
                        //((Editor)m_parent).UseGlobalVarCheckbox.Checked = false;
                    }
                    else
                    {
                        ((Editor)m_parent).SoundEditTypeComboBox.Enabled = true;

                        EventSound selectedSound = ((Editor)m_parent).GetEventTree().GetEventSound(((Editor)m_parent).EventTree.SelectedNode.Parent.Text, ((Editor)m_parent).EventTree.SelectedNode.Index);

                        //if (selectedSound.m_isUsingGlobalVar4Pan || selectedSound.m_isUsingGlobalVar4Volume)
                        if (selectedSound.IsUsingVar(EDIT_TYPE.EDIT_PAN) || selectedSound.IsUsingVar(EDIT_TYPE.EDIT_VOLUME) ||
                            selectedSound.IsUsingVar(EDIT_TYPE.EDIT_REVERB) || selectedSound.IsUsingVar(EDIT_TYPE.EDIT_DISTORTION))
                        {
                            ((Editor)m_parent).SoundEditorTrackbar.Enabled = true;
                            //((Editor)m_parent).UseGlobalVarCheckbox.Enabled = true;
                            ((Editor)m_parent).SoundGlobalVarComboBox.Enabled = true;
                            //((Editor)m_parent).UseGlobalVarCheckbox.Checked = true;
                        }
                        else
                        {
                            ((Editor)m_parent).SoundEditorTrackbar.Enabled = false;
                            //((Editor)m_parent).UseGlobalVarCheckbox.Enabled = true;
                            ((Editor)m_parent).SoundGlobalVarComboBox.Enabled = false;
                            //((Editor)m_parent).UseGlobalVarCheckbox.Checked = false;
                        }
                    }

                    if (((Editor)m_parent).SoundGlobalVarComboBox.Enabled)
                    {
                        for (int j = 0; j < ((Editor)m_parent).GlobalVariableDataGrid.Rows.Count; ++j)
                        {
                            if (((Editor)m_parent).GlobalVariableDataGrid.Rows[j].Cells[0].Value.ToString() == 
                                ((Editor)m_parent).SoundGlobalVarComboBox.Text)
                            {
                                ((Editor)m_parent).GlobalVariableDataGrid.Rows[j].Selected = true;
                                break;
                            }
                        }
                    }

                    ((Editor)m_parent).SoundEditorPanel.Invalidate();

                    return;
                }
            }
        }

        public GlobalVariable GetGlobalVariable(string name)
        {
            for (int i = 0; i < m_globalVariables.Count; ++i)
            {
                if (name == ((GlobalVariable)m_globalVariables[i]).m_name)
                {
                    return ((GlobalVariable)m_globalVariables[i]);
                }
            }
            return new GlobalVariable("poop");//null;
        }

            // Returns the value of the variable with the given name
        public float GetValue(string name)
        {
            for (int i = 0; i < m_globalVariables.Count; ++i)
            {
                if (name == ((GlobalVariable)m_globalVariables[i]).m_name)
                {
                    return ((GlobalVariable)m_globalVariables[i]).m_value;
                }
            }

            return -1.0f;
        }

            // Sets the value of the variable with the given name
        public void SetValue(string name,float value)
        {
            for (int i = 0; i < m_globalVariables.Count; ++i)
            {
                if (name == ((GlobalVariable)m_globalVariables[i]).m_name)
                {
                    ((GlobalVariable)m_globalVariables[i]).m_value = value;

                    DataGridViewRowCollection rows = ((Editor)m_parent).GlobalVariableDataGrid.Rows;

                    for (int r = 0; r < rows.Count; ++r)
                    {
                        if (name == rows[r].Cells[0].Value.ToString())
                        {
                            rows[r].Cells[1].Value = value;
                        }
                    }
                }
            }
        }
    }
}
